package com.ning.parking.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ning.parking.domain.ParkingLotEmpApplyCar;
import com.ning.parking.domain.ParkingRecord;
import com.ning.parking.domain.VO.Pagination;
import com.ning.parking.domain.dto.ParkingLotEmpApplyCarDto;
import com.ning.parking.service.ParkingLotEmpApplyCarService;
import com.ning.parking.mapper.ParkingLotEmpApplyCarMapper;
import com.ning.parking.service.ParkingRecordService;
import com.scsonic.common.enums.parking.ParkingStatusEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
@Slf4j
@AllArgsConstructor
public class ParkingLotEmpApplyCarServiceImpl extends ServiceImpl<ParkingLotEmpApplyCarMapper, ParkingLotEmpApplyCar>
    implements ParkingLotEmpApplyCarService{

    final ParkingRecordService parkingRecordService;

    @Override
    public Pagination<ParkingRecord> findMyRecord(ParkingLotEmpApplyCarDto parkingLotEmpApplyCar) {
        List<ParkingLotEmpApplyCar> list = this.lambdaQuery().eq(ParkingLotEmpApplyCar::getOpenId, parkingLotEmpApplyCar.getOpenId()).list();

        if (ObjectUtil.isEmpty(list)) {
            return Pagination.empty();
        }

        List<String> cars = new ArrayList<>();
        for (ParkingLotEmpApplyCar lotEmpApplyCar : list) {
            cars.add(lotEmpApplyCar.getCarLicense());
        }
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(ParkingStatusEnum.TO_BE_LEAVE);
        objects.add(ParkingStatusEnum.IN_PARKING);
        objects.add(ParkingStatusEnum.LEAVE);
        Page<ParkingRecord> page = parkingRecordService.lambdaQuery()
                .in(ParkingRecord::getCarLicense, cars)
                .in(ParkingRecord::getParkingStatus, objects)
                .orderByDesc(ParkingRecord::getExitTime)
                .page(parkingLotEmpApplyCar.toPage());

        return Pagination.of(page);
    }
}




